<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="password" v-model="ruleForm.password"
                              @keyup.enter.native="submitForm('ruleForm')">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                 <el-form-item prop="rightCode" >
                    <el-input v-model="ruleForm.rightCode" placeholder="请输入验证码" style="width: 60%"></el-input>
                    <span  style="vertical-align:bottom;width: 40%;margin-bottom: 22px">
                        <img src="" ref="code" @click="changeCode">
                    </span>

                </el-form-item>

                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>

                </div>

                <a :href="giteeUrl"
                   target="_self"
                   class="buttonText">码云</a>


            </el-form>
        </div>
    </div>
</template>

<script>
    import {AjaxByAll, apiPath, GetUid,GLobal_SystemId} from '@/api/api';

    export default {

        data: function () {
            return {
                ruleForm: {
                    id: "",
                    username: 'admin',
                    password: '123456',
                    rightCode: '',
                    systemCode:GLobal_SystemId,
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ] ,rightCode: [
                        {required: true, message: '请输入验证码', trigger: 'blur'}
                    ]
                },
                giteeUrl: null,
            }
        },
        methods: {
            changeCode(){
                this.$refs.code.setAttribute(
                    "src",
                    apiPath+ "/sys/login/createCaptcha?id=" +
                    this.ruleForm.id
                );
            },
            GetCurrentUser(update = true) {
                let user = localStorage.getItem('user');
                let token = localStorage.getItem('token');
                if (token && update) {
                    this.$router.push({
                        path: '/'
                    });
                } else {
                    AjaxByAll('get', 'sys/userinfo/getUserInfo', null).then(data => {

                        if (data.code === 200) {
                            localStorage.setItem('user', JSON.stringify(data.data));
                            this.$router.push({
                                path: '/projectList'
                            });
                        } else {
                            this.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    });
                }
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        AjaxByAll('post', 'sys/login/loginSystem', this.ruleForm).then(data => {

                            if (data.code === 200) {
                                if (data.data) {
                                    localStorage.setItem('token', data.data);

                                    this.$router.push({
                                        path: '/projectList'
                                    });

                                } else {
                                    this.changeCode();
                                    this.$message({
                                        message: 'token丢失',
                                        type: 'error'
                                    });
                                }

                            } else {
                                this.changeCode();
                                this.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }

                        });

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }

        },
        mounted: function () {
            this.ruleForm.id = GetUid();
            this.giteeUrl = apiPath + "/oauth/render/gitee";
           this.changeCode();
        },

    }
</script>

<style scoped>
    .login-wrap {
        position: relative;
        width: 100%;
        height: 100%;
        background-image: url(../assets/img/login-bg.jpg);
        background-size: 100%;
    }

    .ms-title {
        width: 100%;
        line-height: 50px;
        text-align: center;
        font-size: 20px;
        color: #fff;
        border-bottom: 1px solid #ddd;
    }

    .ms-login {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 350px;
        margin: -190px 0 0 -175px;
        border-radius: 5px;
        background: rgba(255, 255, 255, 0.3);
        overflow: hidden;

    }

    .ms-content {
        padding: 30px 30px;
    }

    .login-btn {
        text-align: center;
    }

    .login-btn button {
        width: 100%;
        height: 36px;
        margin-bottom: 10px;
    }

    .login-tips {
        font-size: 12px;
        line-height: 30px;
        color: #fff;
    }
    *{
        vertical-align:bottom;
    }
</style>
